require './gookup-jruby/build/artifacts'
require './gookup-jruby/build/constants'
require './gookup-jruby/build/helpers/packging'
require './gookup-jruby/build/templates/template_producer'
require 'rubygems'
require 'yaml'
require 'find'
require 'rake'
require 'rake/clean'
require 'rake/gempackagetask'
require 'fileutils'
require 'spec/rake/spectask'
require 'rubygems/dependency_installer'

repos= %w(http://www.ibiblio.org/maven2/ http://its-subversion.usc.edu/maven2/public/
                 https://maven-repository.dev.java.net/ http://repo1.maven.org/maven2/
                 http://tanesha.net/maven2/).map { |url| Repository.new(url)}

spec = Gem::Specification.new do |s|
  s.name = "gookup"
  s.version = "0.0.5"
  s.date = "2009-02-13"
  s.summary = "Google backup utility"
  s.email = "narkisr@gmail.com"
  s.homepage = "http://code.google.com/p/gookup/"
  s.description = "A Google services backup utiliy (reader, gmail, blogger)."
  s.has_rdoc = false
  s.authors = ["Ronen Narkis"]
  s.files = FileList["{#{Bin}*,#{Lib}lib/*,#{Lib}gookup/**/*}"]
  s.test_files = []
  s.rdoc_options = []
  s.extra_rdoc_files = []
  s.bindir = Bin
  s.executables =['gookup']
  s.default_executable = 'gookup'
end


prod=TemplateProducer.new

CLEAN.include("pkg",Bin,Lib,OUTPUT_PATH)

desc "Builds the required pck lib folders required for the gookup gem."
task :pre_gem =>[:clean]  do
  FileUtils.mkdir_p(Lib+"lib")
  FileUtils.mkdir(Bin)
  FileUtils.cp_r('gookup-jruby/src/gookup/', Lib)# copying the JRuby code as is
  clear_dot_svn_from(Lib)
  clear_dot_svn_from(Bin)
  
  Artifact.reslove_artifacts_paths(PACKAGED).each{|path| FileUtils.cp(path, Lib+"lib")}# copying the Jar's
  File.open(Lib+REQUIRED_JARS, 'w'){ |file| # creating a new required Jars file
    file.puts PACKAGED.flatten.inject([]){|r, a|
      hash=Artifact.new(a).attr
      r << 'lib/'+hash[:id]+'-'+hash[:version]+'.jar'
    }.to_yaml
  }
  File.open("#{Bin}/gookup","w") do |file|
    file.puts "#!/usr/bin/env ruby"
    IO.readlines('gookup-jruby/src/gookup/cmd/gookup.rb').each do |line|
      file.puts line
    end
  end
end

desc "Packages the gookup application."
task :package =>[:resolve_gems,:resolve_java_deps,:spec,:pre_gem,:gem]  do
  Dir.mkdir(OUTPUT_PATH)
  ['/lib', '/bin', '/.jruby'].each{|d| Dir.mkdir(OUTPUT_PATH+d)}
  prod.produce_bin(GEMS+[GOOKUP_GEM],Artifact.new(JRUBY[0]).attr[:version],OUTPUT_PATH+'/bin')
  FileUtils.cp("#{File.dirname(__FILE__)}/pkg/#{GOOKUP_GEM}.gem", OUTPUT_PATH+'/lib')
  GEMS.each{ |gem| FileUtils.cp(LOCAL_GEMS_PATH+gem+'.gem', OUTPUT_PATH+'/lib')}
  FileUtils.cp(Artifact.reslove_artifacts_paths(JRUBY), OUTPUT_PATH+'/lib')
end

desc "Run all specs in spec directory (excluding plugin specs)"
Spec::Rake::SpecTask.new()  do |t|
  t.spec_files = FileList['./gookup-jruby/test//**/*_spec.rb']
end

desc "Creates a gem"
Rake::GemPackageTask.new(spec) do |pkg|
  pkg.need_zip = false
  pkg.need_tar = false
end

desc "Downloads all the required java artifacts"
task :resolve_java_deps do
  arts = [PACKAGED,JRUBY].flatten.map{|s| Artifact.new(s)}
  arts.each{|art|
    repo = repos.find{|repo|repo.fetch(art)} unless File.exist?(LOCAL_REPO+art.path)
    puts "Failed to download #{art.to_s} (no mathing artifact found)." unless File.exist?(LOCAL_REPO+art.path)
  }
end

desc "Downloads all the packaged gems"
task :resolve_gems do
  inst=Gem::DependencyInstaller.new
  search=Gem::GemPathSearcher.new
  installed = GEMS+TEST_GEMS
  installed.each{|gem|
    gem_name=gem.match(/([a-z]|-)*[a-z]+/)[0]
    gem_version=gem.match(/[\d|\.]+/)[0]
    inst.install(gem_name,gem_version) unless search.find(gem_name)
  }
end